/*
 * @Author: FengTeng
 * @Date: 2021-02-12 08:48:10
 * @LastEditTime: 2021-03-03 15:33:03
 * @LastEditors: FengTeng
 * @Description: Description
 * @FilePath: \spa-manage\src\router\index.js
 */
import Vue from "vue";
import VueRouter from "vue-router";
import { authPermissions } from "@/store/auth.permission.js";
import store from "@/store/index.js";
// import utils from "@/utils/init.permissions.js";

Vue.use(VueRouter);

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    // 重复路由回调事件重写
    console.log("重复路由", err.toString().split('"')[1]);
  });
};

const routes = [
  {
    path: "/",
    redirect: "/account/login"
  },
  {
    path: "/account/login",
    name: "Login",
    component: () => import(/* webpackChunkName: "Login" */ "@/views/account/login/Login.vue")
  },
  {
    path: "/main-menu",
    name: "MainMenu",
    component: () => import(/* webpackChunkName: "MainMenu" */ "@/views/main-menu/MainMenu.vue"),
    children: [
      {
        path: "customer-pool",
        name: "CustomerPool",
        component: () => import(/* webpackChunkName: "CustomerPool" */ "@/views/customer-pool/CustomerPool.vue")
      },
      {
        path: "member-manage",
        name: "MemberManage",
        component: () => import(/* webpackChunkName: "MemberManage" */ "@/views/member-manage/MemberManage.vue")
      },
      {
        path: "member-info",
        name: "MemberInfo",
        component: () => import(/* webpackChunkName: "MemberInfo" */ "@/views/member-info/MemberInfo.vue")
      },
      {
        path: "products-manage",
        name: "ProductsManage",
        component: () => import(/* webpackChunkName: "ProductsManage" */ "@/views/products-manage/ProductsManage.vue")
      },
      {
        path: "system-manage",
        name: "SystemManage",
        component: () => import(/* webpackChunkName: "SystemManage" */ "@/views/system-manage/SystemManage.vue")
      }
    ]
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  console.log(to, from, next);
  next();
  if (to.name === "Login") {
    next();
    return;
  }
  console.log(to, from, store.state);
  console.log(authPermissions);
  let permissions = { ...store.state.permissionModule.permissions };
  if (Object.keys(permissions).length === 0) {
    permissions = JSON.parse(sessionStorage.getItem("state"));
    // if (Object.keys(permissions).length === 0) {
    //   permissions = utils.initPermissions();
    // }
  }
  let hasPermission = false;
  if (Object.keys(permissions).length !== 0 && to.name === "MainMenu") {
    next();
    return;
  }
  if (to.name === "CustomerPool") {
    if (permissions.customerPool && permissions.customerPool.lookSearch) {
      hasPermission = true;
    }
    console.log("客户池");
  } else if (to.name === "MemberManage") {
    if (permissions.customerPool && permissions.memberManage.lookSearch) {
      hasPermission = true;
    }
    console.log("会员管理");
  } else if (to.name === "MemberInfo") {
    if (permissions.customerPool && permissions.memberInfo.lookSearch) {
      hasPermission = true;
    }
    console.log("会员画像");
  } else if (to.name === "ProductsManage") {
    if (permissions.customerPool && permissions.productsManage.lookSearch) {
      hasPermission = true;
    }
    console.log("商品管理");
  } else if (to.name === "SystemManage") {
    if (permissions.customerPool && permissions.systemManage.lookSearch) {
      hasPermission = true;
    }
    console.log("系统管理");
  }
  if (hasPermission) {
    next();
  } else if (!hasPermission && Object.keys(permissions).length !== 0) {
    next({ path: "/main-menu" });
    return;
  } else {
    next({ path: "/account/login" });
  }
});

export default router;
